package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.qurey.TravelCondition;
import cn.wolfcode.wolf2w.qurey.TravelQuery;
import cn.wolfcode.wolf2w.repository.TravelRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.DBHepler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author 857
 * @date
 */
@Service
//@Transactional mongodb事务必须复制集方式
public class TravelServiceImpl implements ITravelService {

    @Autowired
    private TravelRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IDestinationService destinationService;


    @Override
    public void save(Travel Travel) {
        Travel.setId(null);
        repository.save(Travel);
    }

    @Override
    public void update(Travel Travel) {
        repository.save(Travel);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public Travel get(String id) {
        Optional<Travel> byId = repository.findById(id);
        Travel travel = byId.get();
        if (travel != null) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
            Destination destination = destinationService.get(travel.getDestId());
            System.out.println(destination);

            travel.setDest(destinationService.get(travel.getDestId()));
        }

        System.out.println(travel);
        return travel;
    }

    @Override
    public List<Travel> list() {
        return repository.findAll();
    }

    @Override
    public Page<Travel> query(TravelQuery qo) {
        Query query = new Query();
        if (qo.getState() != -1) {
            query.addCriteria(Criteria.where("state").is(qo.getState()));
        }

        if (StringUtils.hasLength(qo.getDestId())) {
            query.addCriteria(Criteria.where("destId").is(qo.getDestId()));
        }

        //旅游天数
        TravelCondition day = TravelCondition.DAYMAP.get(qo.getDayType());
        if (day != null) {
            query.addCriteria(Criteria.where("day").gte(day.getMin()).lte(day.getMax()));
        }
        //人均消费
        TravelCondition ex = TravelCondition.PEREXPENDMAP.get(qo.getPerExpendType());
        if (ex != null) {
            query.addCriteria(Criteria.where("perExpend").gte(ex.getMin()).lte(ex.getMax()));
        }

        Pageable pageable = pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        if (qo.getOrderType() == 1) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                    Sort.Direction.DESC, "createTime");
        } else if (qo.getOrderType() == 2) {
            pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                    Sort.Direction.DESC, "viewnum");
        }

        Page<Travel> page = DBHepler.query(template, pageable, query, Travel.class, qo);
        //页面确实用户对象[维护用户对象]
        for (Travel travel : page) {
            travel.setAuthor(userInfoService.get(travel.getUserId()));
        }
        return page;
    }

    @Override
    public void saveOrUpdate(Travel travel) {
        //目的地名称
        Destination dest = destinationService.get(travel.getDestId());
        travel.setDestName(dest.getName());

        //用户id, 表现层controller设置了
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        if (StringUtils.hasLength(travel.getId())) {
            this.update(travel);
        } else {
            //创建时间
            travel.setCreateTime(new Date());
            this.save(travel);
        }
    }

    @Override
    public void audit(String id, int state) {
        //判断是否满足审核状态
        Travel travel = this.get(id);

        if (travel == null || travel.getState() != Travel.STATE_WAITING) {
            throw new LogicException("参数异常");
        }

        if (state == Travel.STATE_RELEASE) {
            //审核通过后要做什么
            //1.游记状态改为,审核通过/发布
            travel.setState(Travel.STATE_RELEASE);
            //2.游记发布时间为当前时间
            travel.setReleaseTime(new Date());
            //3.游记最后修改时间
            travel.setLastUpdateTime(new Date());
            //4.记录审核信息:审核人 审核时间 审核建议
        } else if (state == Travel.STATE_REJECT) {
            //审核拒绝后要做什么
            //1.游记状态改为,审核通过/发布
            travel.setState(Travel.STATE_REJECT);
            //2.游记发布时间为null
            travel.setReleaseTime(null);
            //3.游记最后修改时间
            travel.setLastUpdateTime(new Date());
            //4.记录审核信息:审核人 审核时间 审核建议
        } else {
            throw new LogicException("参数异常");
        }
        this.update(travel);
    }

    @Override
    public List<Travel> queryViewnumTop3(String destId) {
        PageRequest pageable = PageRequest.of(0, 3, Sort.Direction.DESC, "viewnum");
        List<Travel> byDestId = repository.findByDestId(destId, pageable);
        return byDestId;
    }

    @Override
    public void updateState(String id, String state) {
        if (StringUtils.hasLength(id)){
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(id));
            Update update = new Update();
            update.set("state", state);
            template.updateMulti(query,update ,Travel.class );
        }
    }

    @Override
    public List<Travel> findByDestId(String id) {
        List<Travel> byDestId = repository.findByDestId(id, null);
        for (Travel travel : byDestId) {
            if (travel != null) {
                travel.setAuthor(userInfoService.get(travel.getUserId()));
                Destination destination = destinationService.get(travel.getDestId());
                travel.setDest(destinationService.get(travel.getDestId()));
            }
        }
        return byDestId;
    }
}
